﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace QuanLyTuyenSinh.PresentationLayer.Components
{
    public partial class frmPhanphong : Form
    {
        private SqlCommand Cmd;
        private SqlConnection Conn;
        private SqlDataAdapter DTA;
        private DataTable DTL;
        private DataSet DTS;
        private string Sqlx;

        private int dem = 1;
        private int sp = 1;

        public frmPhanphong()
        {
            InitializeComponent();
        }

        private void Updatehs(string SBD, string PhongThi)
        {
            try
            {
                //Conn = ConnectData.Getcon();
                //Conn.Open();
                //string sqlk = "Update " + comboKhoithi.Text.Trim() + " set Phongthi='" + PhongThi + "' where SBD='" + SBD + "'";
                //var cmd = new SqlCommand(sqlk, Conn);
                //cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //private DataTable GetByAll()
        //{
        //    //Sqlx = "select str(row_number() over(order by " + comboKhoithi.Text.Trim() + ".SBD)) as 'STT',Khoithi,Phongthi,SBD,Hodem,Ten,Ngaysinh,Manganh from " + comboKhoithi.Text;
        //    //var cmd = new SqlCommand(Sqlx, ConnectData.Getcon());
        //    //return ConnectData.GetTable(cmd);
        //    return ;
        //}

        private void frmPhanphong_Load(object sender, EventArgs e)
        {
            //Conn = ConnectData.Getcon();
            //Conn.Open();
            //comboKhoithi.DataSource = monthibus.GetKhoithiUngVoiNganhHoc();
            //comboKhoithi.DisplayMember = "Khoithi";
            //comboKhoithi.ValueMember = "Khoithi";
            //sp = 1;
            //txtSoTSPhong.Clear();
        }

        private void comboKhoithi_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                txtSoTSPhong.Clear();
                Sqlx = "select str(row_number() over(order by " + comboKhoithi.Text.Trim() + ".SBD)) as 'STT',Khoithi,Phongthi,SBD,Hodem,Ten,Ngaysinh,Manganh from " + comboKhoithi.Text;
                DTA = new SqlDataAdapter(Sqlx, Conn);
                DTS = new DataSet();
                DTA.Fill(DTS, comboKhoithi.Text.Trim());
                DTL = new DataTable();
                DTL = DTS.Tables[comboKhoithi.Text.Trim()];
                if (DTL.Rows.Count > 0)
                    DataGridPhong.DataSource = DTL;
                DataGridPhong.Columns[1].HeaderText = "Khối thi";
                DataGridPhong.Columns[2].HeaderText = "Phòng";
                DataGridPhong.Columns[3].HeaderText = "SBD";
                DataGridPhong.Columns[4].HeaderText = "Họ đệm";
                DataGridPhong.Columns[5].HeaderText = "Tên";
                DataGridPhong.Columns[6].HeaderText = "Ngày sinh";
                DataGridPhong.Columns[7].HeaderText = "Mã ngành";
            }
            catch (Exception ex)
            {
                return;
                //MessageBox.Show(ex.ToString());
            }
        }

        private void cmdPhanphong_Click(object sender, EventArgs e)
        {
            try
            {
                sp = 1;
                dem = 1;
                if ((txtSoTSPhong.Text == "") || (int.Parse(txtSoTSPhong.Text) <= 0) || (int.Parse(txtSoTSPhong.Text) > 40))
                {
                    MessageBox.Show("Số lượng thí sinh trong mỗi phòng lớn hơn 0 và nhỏ hơn 40");
                }
                else
                {
                    //foreach (DataRow row in GetByAll().Rows)
                    //{
                    //    if (dem < int.Parse(txtSoTSPhong.Text))
                    //    {
                    //        string Id = row["SBD"].ToString();
                    //        Updatehs(Id, sp.ToString());
                    //        dem++;
                    //    }
                    //    else
                    //    {
                    //        string Id = row["SBD"].ToString();
                    //        Updatehs(Id, sp.ToString());
                    //        dem = 1;
                    //        sp += 1;
                    //    }
                    //}
                    sp = 1;
                    dem = 1;
                    //DataGridPhong.DataSource = GetByAll();
                    MessageBox.Show("Phân phòng thi cho khối " + comboKhoithi.Text + " thành công!");
                    Sqlx = " delete TSSBD insert into TSSBD select * from A " +
                           " insert into TSSBD select * from B " +
                           " insert into TSSBD select * from C " +
                           " insert into TSSBD select * from D1 " +
                           " insert into TSSBD select * from D4 " +
                           " insert into TSSBD select * from M " +
                           " insert into TSSBD select * from T ";

                    Cmd = new SqlCommand(Sqlx, Conn);
                    Cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}